<?php include '../public/header.php' ?>
<?php include '../public/header_index.php' ?>
<style>
    div[lay-id="datalist"] .layui-table-cell{height:auto !important;};
    .laytp-container .xmselect_div{border-color: #eee;}
    input{border-color: #ccc !important;}
    .laytp-container xm-select{min-height: 36px !important;line-height: 36px !important;}
    .xmselect_title{font-size: 14px;line-height: 36px;padding-left: 5px;}
    body{padding:20px;padding-top: 0;}.multi dl dd.layui-this{background-color:#fff !important;}
</style>
<body class="laytp-container">
    <div id="opt_nav">
    </div>

    <div class="layui-fluid layui-fluid_list">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card" style="padding-top: 20px;padding-bottom: 20px;">
                    <div class="layui-card-body">
                        <div class="layui-form layui-col-space5 search_fors">
                            <div class="layui-inline layui-show-xs-block">
                                <div id="type" class="xmselect_div" style="width: 195px;height: 36px;">
                                    <div class="xmselect_title">证书分类</div>
                                </div>
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <select id="name" multiple="multiple" lay-filter="name"><option value="">证书名称</option></select>
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <select id="major" multiple="multiple"><option value="">证书专业</option></select>
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <select id="xueli" multiple="multiple"><option value="">学历</option></select>
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <select id="social"  multiple="multiple"><option value="">社保状态</option></select>
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <select id="nature"><option value="">人员性质</option></select>
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <select id="goes"><option value="">证书去向</option></select>
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <select id="statueuse" multiple="multiple"><option value="">使用情况</option></select>
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <select id="chanid"><option value="">归属公司</option></select>
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <button class="layui-btn btn-dark" type="button" id="add_search_cond"><i class="layui-icon">&#xe608;</i>添加筛选条件</button>
                            </div>

                            <!--条件展示-->
                            <div style="border: 1px solid #e3e3e3;min-height: 150px;">
                                <div style="width: 100%;min-height: 100px;padding:10px 0px 0px 10px;" id="condition_list">
                                    <span id="tips" style="font-size:18px;color:#9c9c9c;">请先添加筛选条件</span>
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-input-block" style="margin-left: 40px;text-align:center;">
                                        <input type="radio" name="is_both" value="1" title="任意均可" checked lay-filter="is_both">
                                        <input type="radio" name="is_both" value="2" title="同时具备" lay-filter="is_both">
                                    </div>
                                </div>
                            </div>
                            <div>
                                <div class="layui-form-item">
                                    <div class="layui-input-block" style="margin-left: 40px;text-align:center;">
                                        <button class="layui-btn btn-dark" id="search"><i class="layui-icon">&#xe615;</i>查询</button>
                                        <button class="layui-btn btn-dark" onclick="location.reload()" ><i class="layui-icon">&#xe633;</i>重置</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>


                    <div class="layui-card-body">
                        <span id="count_row" class="layui-hide">查出符合要求的证书有<span id="count_num">0</span>本</span>
                        <button class = "layui-btn layui-btn-sm btn-dark" lay-event="choose" id="choose"><i class="layui-icon">&#xe654;</i>加入已选</button>
                        <button class = "layui-btn layui-btn-sm btn-dark" lay-event="choose_ids" id="choose_ids">已选: 0</button>
                    </div>


                    <div class="layui-card-body">
                        <div id="datalist" lay-filter="datalist"></div>
                    </div>

                    <input type="hidden" id="change" value="">
                </div>
            </div>
        </div>
    </div>
</body>


<script type="text/html" id="test-dropdown-toolbar-barDemo">
    <div class="tab-list-authbut">
        <a href="javascript:;" class="bar_opt" lay-event="detail">更换</a>
    </div>
</script>
<script type="text/html" id="toolbarDemo">
    <div class = "layui-btn-container">
    </div>
</script>

<script type="text/javascript">
    // 添加筛选条件
    var check_repeat_list = [];
    var list = [''];
    $('#add_search_cond').click(function () {
        var type = ''; // 证书分类
        var type_s = '';
        $('#type').find('.xm-tree>div').each(function () {
            if($(this).attr('class') == 'xm-option   selected show-icon'){
                type = $(this).attr('value');
                let title = $(this).find('div').text();
                type_s += type_s === '' ? title : ','+title;
            }
        });

        var name = []; // 证书名称
        var name_s = '';
        $('#name').parent().find('dd').each(function() {
            if($(this).find('div').attr('class') == 'layui-unselect layui-form-checkbox layui-form-checked'){
                let title = $(this).find('input').attr('title');
                name.push(title);
                name_s += name_s === '' ? title : ','+title;
            }
        });

        var major = [];
        var major_s = '';
        $('#major').parent().find('dd').each(function() {
            if($(this).find('div').attr('class') == 'layui-unselect layui-form-checkbox layui-form-checked'){
                let title = $(this).find('input').attr('title');
                major.push(title);
                major_s += major_s === '' ? title : ','+title;
            }
        });

        var xueli = []; // 学历
        var xueli_s = '';
        $('#xueli').parent().find('dd').each(function() {
            if($(this).find('div').attr('class') == 'layui-unselect layui-form-checkbox layui-form-checked'){
                let title = $(this).find('input').attr('title');
                let val = $(this).attr('lay-value');
                xueli.push(val);
                xueli_s += xueli_s === '' ? title : ','+title;
            }
        });

        var social = []; // 社保状态
        var social_s = '';
        $('#social').parent().find('dd').each(function() {
            if($(this).find('div').attr('class') == 'layui-unselect layui-form-checkbox layui-form-checked'){
                let title = $(this).find('input').attr('title');
                let val = $(this).attr('lay-value');
                social.push(val);
                social_s += social_s === '' ? title : ','+title;
            }
        });

        var nature = $('#nature').val(); // 人员性质
        var nature_s = $('#nature option:selected').text();

        var goes = $('#goes').val(); // 证书去向
        var goes_s = $('#goes option:selected').text();


        var statueuse = []; // 使用情况
        var statueuse_s = '';
        $('#statueuse').parent().find('dd').each(function() {
            if($(this).find('div').attr('class') == 'layui-unselect layui-form-checkbox layui-form-checked'){
                let title = $(this).find('input').attr('title');
                let val = $(this).attr('lay-value');
                statueuse.push(val);
                statueuse_s += statueuse_s === '' ? title : ','+title;
            }
        });


        var chanid = $('#chanid').val(); // 所属公司
        var chanid_s = $('#chanid option:selected').text();


        if(type_s == ''){
            layer.msg('请选择证书分类'); return false;
        }
        // 页面展示
        var check_repeat_str = '';
        check_repeat_str += '【'+type_s+'】';
        if(name_s != ''){
            check_repeat_str += '【'+name_s+'】';
        }
        if(major_s != ''){
            check_repeat_str += '【'+major_s+'】';
        }
        if(xueli_s != ''){
            check_repeat_str += '【'+xueli_s+'】';
        }
        if(social_s != ''){
            check_repeat_str += '【'+social_s+'】';
        }
        if(nature != ''){
            check_repeat_str += '【'+nature_s+'】';
        }
        if(goes != ''){
            check_repeat_str += '【'+goes_s+'】';
        }
        if(statueuse_s != ''){
            check_repeat_str += '【'+statueuse_s+'】';
        }

        if(chanid != ''){
            check_repeat_str += '【'+chanid_s+'】';
        }

        // 判断条件是否重复添加
        var check_repeat = 0;
        for (var i=0;i<check_repeat_list.length;i++){
            if(check_repeat_list[i] == check_repeat_str){
                layer.msg('查询添加不能重复'); check_repeat = 1;
            }
        }
        if(check_repeat === 0){
            check_repeat_list.push(check_repeat_str);
        }else{
            return false;
        }
        // 数据处理
        var con = {
            type:type,
            name:name,
            major:major,
            xueli:xueli,
            social:social,
            nature:nature,
            goes:goes,
            statueuse:statueuse,
            chanid:chanid
        };
        var json_con = JSON.stringify(con);
        list.push(json_con);

        // 页面展示
        $('#condition_list').append('<div check_repeat='+check_repeat_str+'>'+check_repeat_str+'&nbsp;&nbsp;'+
            '<span style="color:red;cursor:pointer;" class="del_condition">X</span></div>');
        // 隐藏条件提示
        $('#tips').addClass('layui-hide');
    });

    // 删除筛选条件
    $('#condition_list').on('click','.del_condition',function () {
        var check_repeat_str = $(this).parent().attr('check_repeat');
        var index = $.inArray(check_repeat_str,check_repeat_list);
        check_repeat_list.splice(index,1);
        $(this).parent().remove();
        // 删除数据
        list.splice(index+1, 1);
        // 显示添加条件的提示
        if(check_repeat_list.length == 0 || check_repeat_list.length == undefined){
            $('#tips').removeClass('layui-hide');
        }
    });

    // 子页面调用查询条件
    function set_list(){
        return list;
    }


    // 下载保存的id集合  判断是否有流程
    var idArr = [];  var flow = 0;
    // 下载页面删除，动态赋值
    function set_idArr(ids){
        idArr = ids;
        $('#choose_ids').html('已选：'+idArr.length);
    }
    // 子页面获取数据
    function get_idArr(){
        return idArr;
    }
    // 打开投标用证申请页面
    function open_cert_use_bid() {
        layer.closeAll();
        // if(flow == 1){
            openPageEdit('../cert_use_bid/flow_add.php?idArr='+JSON.stringify(idArr)+'&close=1&type=1');
        // }else{
        //     openPageEdit('../cert_use/add.php?idArr='+JSON.stringify(idArr)+'&close=1&type=1');
        // }
    }



    var sea_type = '', sea_type_title='';
    var is_hide = false;
    layui.use(['form','element','table','multiSelect'],function() {
        var form = layui.form;
        var table = layui.table;
        var multiSelect = layui.multiSelect;

        // 初始化数据
        facade.ajax({type: 'get', url: ask_cert_matching_list}).done(function(e){
            if (e.code === 2000) {
                flow = e.data.flow;

                xmselect_dan('type', e.data.type, sea_type, function (res) {
                    $('.xm-tips').html('证书分类');
                    sea_type = res.arr && res.arr.length > 0 ?  res.arr[0].value : '';
                    sea_type_title = res.arr && res.arr.length > 0 ? res.arr[0].name : '';

                    if(res.arr && res.arr.length > 0){
                        facade.ajax({type: 'get', url: ask_cert_form_name_list,data:{type:res.arr[0].value}}).done(function(e){
                            if (e.code === 2000) {
                                var cert_name = e.data;
                                var cert_name_str = '<option value="">证书名称</option>';
                                for (var i=0;i<cert_name.length;i++){
                                    cert_name_str += '<option value="'+cert_name[i]+'">'+cert_name[i]+'</option>';
                                }
                                $('#name').html(cert_name_str);
                                $('#major').html('<option value="">证书专业</option>');
                                // render_from();
                                multiSelect.render();
                            }
                        });
                    }
                });
                $('.xm-tips').html('证书分类');

                // 学历
                var xueliArr = e.data.xueli;
                var xueli_str= '<option value="">学历</option>';
                for (var i=0;i<xueliArr.length;i++){
                    xueli_str += '<option value="'+xueliArr[i].value+'">'+xueliArr[i].name+'</option>';
                }
                $('#xueli').html(xueli_str);

                // 社保状态
                var socialArr = e.data.social;
                var social_str= '<option value="">社保状态</option>';
                for (var i=0;i<socialArr.length;i++){
                    social_str += '<option value="'+socialArr[i].value+'">'+socialArr[i].name+'</option>';
                }
                $('#social').html(social_str);

                // 使用情况
                var statueuseArr = e.data.statueuse;
                var statueuse_str= '<option value="">使用情况</option>';
                for (var i in statueuseArr){
                    statueuse_str += '<option value="'+i+'">'+statueuseArr[i]+'</option>';
                }
                $('#statueuse').html(statueuse_str);

                multiSelect.render();

                // 证书去向
                var goesArr = e.data.goes;
                var goes_str= '<option value="">证书去向</option>';
                for (var i in goesArr){
                    goes_str += '<option value="'+i+'">'+goesArr[i]+'</option>';
                }
                $('#goes').html(goes_str);

                // 人员性质
                var natureArr = e.data.nature;
                var nature_str= '<option value="">人员性质</option>';
                for (var i=0;i<natureArr.length;i++){
                    nature_str += '<option value="'+natureArr[i].value+'">'+natureArr[i].name+'</option>';
                }
                $('#nature').html(nature_str);




                // 所属公司
                var chanidArr = e.data.chanid;
                var chanid_str = '<option value="">归属公司</option>';
                for (var i=0;i<chanidArr.length;i++){
                    chanid_str += '<option value="'+chanidArr[i].id+'">'+chanidArr[i].name+'</option>';
                }
                $('#chanid').html(chanid_str);
                render_from();
            }
        });

        $('#cols_table').hide();
        $('#export_table').hide();
        $('#search_table').hide();
        table.render({
            elem: "#datalist",id: "datalist"
            ,url: facade.url(ask_cert_matching_list)
            ,where: {is_both:0}, page: false, height: 'full-140'
            ,soulSort: TabInit.soulSort,method: TabInit.method, toolbar: TabInit.toolbar, overflow: TabInit.overflow
            ,defaultToolbar: TabInit.defaultToolbar,filter: TabInit.filter
            ,request: TabInit.request,response: TabInit.response
            ,text: TabInit.text
            ,parseData: function (res) {
                currButAuth = res.data.butauth;
                tableButShow(currButAuth);

                $('#count_num').text(res.data.count);
                $('#count_row').removeClass('layui-hide');

                // render_from();
                return {
                    "code": res.code,
                    "msg": res.message,
                    "data": res.data.data
                };
            }
            ,cols: [[
                {type: 'checkbox',fixed:'left'}
                ,{type: 'numbers', title: '序号',fixed:'left'}
                ,{field: 'person_name', title: '持证人',event:'person_name',templet:function (e) {
                        return '<a class="colorlan">'+e.person_name+'</a>';
                    }}
                ,{field: 'nature', title: '人员性质'}
                ,{field: 'cert_name', title: '证书名称',event:'cert_name',templet:function (e) {
                    return '<a class="colorlan">'+e.cert_name+'</a>';
                }}
                ,{field: 'number', title: '证书编号'}
                ,{field: 'major', title: '证书专业'}
                ,{field: 'expiry_dt', title: '有效期'}
                ,{field: 'issue_unit', title: '发证单位'}
                ,{field: 'chanid_name', title: '归属公司'}
                ,{field: 'goes', title: '证书去向'}
                ,{field: 'state', title: '证书状态'}
                ,{field: 'person_state', title: '人员状态'}
                ,{field: 'social', title: '社保状态'}
                // ,{fixed: 'right', title:'操作', toolbar: '#test-dropdown-toolbar-barDemo',align:'center',width:200}
            ]]
            , done: function (res) {

                // 处理列表fixed的高度问题
                $('div[lay-id="datalist"] .layui-table-main>table>tbody>tr').each(function (i) {
                    var height = $(this).height();
                    $('div[lay-id="datalist"] .layui-table-fixed-l>.layui-table-body>table>tbody>tr')
                        .eq(i).css('cssText','height:'+height+'px !important');
                    $('div[lay-id="datalist"] .layui-table-fixed-r>.layui-table-body>table>tbody>tr')
                        .eq(i).css('cssText','height:'+height+'px !important');
                });
            }
        });


        // 详情
        table.on('tool(datalist)',function (obj) {
            var data = obj.data;
            if(obj.event === 'cert_name'){
                openPage('../cert/show.php?id='+data.id,'100%','100%');
            }else if (obj.event === 'cert_status'){
                openPage('../cert/engin_build.php?id='+data.id,'100%','100%');
            }else if(obj.event === 'detail'){
                layer.open({
                    type: 2,
                    title: '选择证书',
                    area: ['90%', '90%'],
                    maxmin: true,
                    content: 'change.php',
                    end: function () {
                        var change = $('#change').val();
                        var change_data = JSON.parse(change);
                        obj.update(change_data);
                    }
                });
            }else if(obj.event === 'person_name'){
                open_page('../cert_person/show.php?id='+data.personid,'人员详情');
            }
        });


        $('#choose').click(function () {
            var checkStatus = table.checkStatus('datalist');
            var data = checkStatus.data;
            if(data.length === 0){
                layer.msg('请选择证书');return false;
            }
            for(var i=0;i<data.length;i++){
                idArr.push(data[i].id)
            }
            $.unique(idArr.sort())
            $('#choose_ids').html('已选：'+idArr.length);
        });
        $('#choose_ids').click(function () {
            if(idArr.length === 0){
                layer.msg('请选择证书');return false;
            }
            open_page('alloc.php','加入申请');
        });

        // 查询证书，刷新列表
        $('#search').on('click', function(){
            if(list.length === 1){
                layer.msg('请添加查询条件');return false;
            }
            // 获取查询方式 1任意均可 2同时具备
            var is_both = $('input[name="is_both"]:checked').val();
            if(is_both == 1){
                is_hide = false;
            }else{
                is_hide = true;
            }
            table.reload('datalist', {
                where:{
                    is_both:is_both,
                    list: list,
                }
            });
            $('#choose_ids').html('已选：'+idArr.length);
        });


        // 证书分类改变事件
        // form.on('select(type)', function(data){
        //     var id = data.value;
        //     facade.ajax({type: 'get', url: ask_cert_form_name_list,data:{type:id}}).done(function(e){
        //         if (e.code === 2000) {
        //             var cert_name = e.data;
        //             var cert_name_str = '<option value="">证书名称</option>';
        //             for (var i=0;i<cert_name.length;i++){
        //                 cert_name_str += '<option value="'+cert_name[i]+'">'+cert_name[i]+'</option>';
        //             }
        //             $('#name').html(cert_name_str);
        //             $('#major').html('<option value="">证书专业</option>');
        //             // render_from();
        //             multiSelect.render();
        //         }
        //     });
        // });

        // // 证书名称改变事件
        // form.on('select(name)', function(data){
        //     var name = data.value;
        //     facade.ajax({type: 'get', url: ask_cert_form_major_list,data:{name:name,type:sea_type}}).done(function(e){
        //         if (e.code === 2000) {
        //             var cert_major = e.data;
        //             var cert_major_str = '<option value="">证书专业</option>';
        //             for (var i in cert_major){
        //                 cert_major_str += '<option value="'+cert_major[i]+'">'+cert_major[i]+'</option>';
        //             }
        //             $('#major').html(cert_major_str);
        //             render_from();
        //         }
        //     });
        // });



        var cert_class_num = 0; var cert_name_num = 0;
        setInterval(function () {
            // if($('#cert_class').parent().children('div').attr('class') === 'layui-unselect layui-form-select multi'){
            //     if(cert_class_num > 0){
            //         cert_class_num = 0;
            //
            //         var vals = [];
            //         $('#cert_class').parent().find('dd').each(function() {
            //             if($(this).find('div').attr('class') == 'layui-unselect layui-form-checkbox layui-form-checked'){
            //                 vals.push($(this).attr('lay-value'));
            //             }
            //         });
            //
            //         facade.ajax({type: 'get', url: ask_cert_form_name_list,data:{type:vals}}).done(function(e){
            //             if (e.code === 2000) {
            //                 var cert_name = e.data;
            //                 var cert_name_str = '<option value="">证书名称</option>';
            //                 for (var i=0;i<cert_name.length;i++){
            //                     cert_name_str += '<option value="'+cert_name[i]+'">'+cert_name[i]+'</option>';
            //                 }
            //                 $('#name').html(cert_name_str);
            //                 $('#major').html('<option value="">证书专业</option>');
            //                 multiSelect.render();
            //             }
            //         });
            //     }
            // }

            if($('#name').parent().children('div').attr('class') === 'layui-unselect layui-form-select multi'){
                if(cert_name_num > 0){
                    cert_name_num = 0;

                    var vals = [];
                    $('#name').parent().find('dd').each(function() {
                        if($(this).find('div').attr('class') == 'layui-unselect layui-form-checkbox layui-form-checked'){
                            vals.push($(this).find('input').attr('title'));
                        }
                    });

                    var type = '';
                    $('#type').find('.xm-tree>div').each(function () {
                        if($(this).attr('class') == 'xm-option   selected show-icon'){
                            type = $(this).attr('value');
                        }
                    });

                    facade.ajax({type: 'get', url: ask_cert_form_major_list,data:{name:vals,type:type}}).done(function(e){
                        if (e.code === 2000) {
                            var cert_major = e.data;
                            var cert_major_str = '<option value="">证书专业</option>';
                            for (var i in cert_major){
                                cert_major_str += '<option value="'+cert_major[i]+'">'+cert_major[i]+'</option>';
                            }
                            $('#major').html(cert_major_str);
                            multiSelect.render();
                        }
                    });
                }
            }
        },1000);

        // 证书分类改变事件
        // form.on('select(cert_class)', function(data){
        //     cert_class_num = 1;
        // });

        // 证书名称改变事件
        form.on('select(name)', function(data){
            cert_name_num = 1;
        });




        // 1任意均可 2同时具备
        form.on('radio(is_both)', function(data){
            if(data.value == 1){
                is_hide = false;
            }else{
                is_hide = true;
            }
        });

    });
</script>
</html>















